$(function () {
    //获取费用信息
    queryFeeInfo("101");
    //标签管理选中样式
    $('.label-nav-con li').click(function() {
        $(this).addClass('current').siblings().removeClass('current');
        var feeType = $(this).attr("id");
        queryFeeInfo(feeType)
    })
})

//获取费用数据信息列表
function queryFeeInfo(feeType){
    var str="";
    var colInfo;
    $("#contentInfo").empty();
    if(feeType=='104'){
        //如果是伙食费
        str+="<div class=\"border-radius4 bgWhite box-shadows\">";
        str+="<div class=\"scroll-box-con\">";
        str+="<div class=\"oveflow lh32px mar25px\">";
        str+="<div class=\"left\">";
        str+="<label>伙食费（元/天）：</label>";
        str+="</div>";
        str+="<div class=\"left pl15px pr15px border1px border-radius4\">";
        str+="<p><input id='mealFee' onkeyup=\"value=value.replace(/^\\D*(\\d*(?:\\.\\d{0,2})?).*$/g, '$1')\" type=\"text\" placeholder=\"请输入伙食费\" style=\"width:280px;\"></p>";
        str+="</div>";
        str+="</div>";
        str+="<div class=\"btn-con textCenter\">";
        str+="<span onclick='saveMealFeeInfo()' class=\"btn-two cursor\">提&nbsp;交</span>";
        str+="</div>";
        str+="</div>";
        //查询伙食费信息
        $.ajax({
            url:getRootPath_web()+'baseData/fee/queryMealFeeInfo',
            type:'post',
            dataType:'json',
            success:function (result) {
                $("#contentInfo").addClass("layui-right-con-bot");
                $("#contentInfo").append(str);
                $("#mealFee").val(result.mealPrice);
            }
        })
    }else {
        str+="<div class=\"border-radius4 bgWhite box-shadows\">";
        str+="<div class=\"scroll-box-con\">";
        str+="<div class=\"mb20px lh32px overflow add-btn-input\">";
        if(isCURD){
            str+="<p class=\"left cursor\">";
            str+="<span onclick='addFeeInfo()' class=\"white border-radius4 bgGreen inline-block pl15px pr15px\">" +
                "<img src='"+getRootPath_web()+"static/img/button_add.png'>新建</span>";
            str+="</p>";
        }
        str+="<p class=\"right\">共有<span id='totalCount' class=\"red\">0</span>条数据</p>";
        str+="</div>";
        str+="<div class=\"border1px border-radius4 relative mb40px\">";
        str+="<table class=\"layui-hide\" id=\"test\" lay-filter=\"test\"></table>";
        str+="</div>";
        str+="</div>";
        str+="</div>";
        $("#contentInfo").append(str);
        if(feeType=='102'){
            //如果是护理费
            colInfo=  [
                [ //表头
                    {
                        field: 'order',
                        title: '序号',
                        fixed: 'left',
                        width: 80,
                    }, {
                    field: 'costName',
                    title: '护理等级',
                }, {
                    field: 'costPrice',
                    title: '护理费（元/月）',
                }, {
                    field: 'operation',
                    title: '操作',
                    style: 'color:#1C9399;',
                    fixed: 'right',
                    width: 140,
                    templet: function(res){
                        console.log(res);
                        if(isCURD){
                            return "<div class='table-list-btn'><span class='cursor' lay-event=\"edit\">编辑</span>|<span class='cursor' lay-event=\"del\">删除</span></div>"
                        }else {
                            return "<div class='table-list-btn'><span style='color: gray'>编辑</span>|<span style='color: gray'>删除</span></div>"
                        }
                    }
                }
                ]
            ]
        }else if(feeType=='103'){
            //如果是长护险费
            colInfo=  [
                [ //表头
                    {
                        field: 'order',
                        title: '序号',
                        fixed: 'left',
                        width: 80,
                    }, {
                    field: 'costName',
                    title: '评估等级',
                }, {
                    field: 'costPrice',
                    title: '长护险补贴费用（元/月）',
                }, {
                    field: 'operation',
                    title: '操作',
                    style: 'color:#1C9399;',
                    fixed: 'right',
                    width: 140,
                    templet: function(res){
                        console.log(res);
                        if(isCURD){
                            return "<div class='table-list-btn'><span class='cursor' lay-event=\"edit\">编辑</span>|<span class='cursor' lay-event=\"del\">删除</span></div>"
                        }else {
                            return "<div class='table-list-btn'><span style='color: gray'>编辑</span>|<span style='color: gray'>删除</span></div>"
                        }
                    }
                }
                ]
            ]
        }else {
            colInfo=  [
                [ //表头
                    {
                        field: 'order',
                        title: '序号',
                        fixed: 'left',
                        width: 80,
                    }, {
                    field: 'costName',
                    title: '床位类型',
                }, {
                    field: 'costPrice',
                    title: '床位费（元/月）',
                }, {
                    field: 'operation',
                    title: '操作',
                    style: 'color:#1C9399;',
                    fixed: 'right',
                    width: 140,
                    templet: function(res){
                        if(isCURD){
                            return "<div class='table-list-btn'><span class='cursor' lay-event=\"edit\">编辑</span>|<span class='cursor' lay-event=\"del\">删除</span></div>"
                        }else {
                            return "<div class='table-list-btn'><span style='color: gray'>编辑</span>|<span style='color: gray'>删除</span></div>"
                        }
                    }
                }
                ]
            ]
        }
        layui.use(['table'], function() {
            var table = layui.table;
            //第一个实例
            table.render({
                elem: '#test',
                url:getRootPath_web()+'baseData/fee/obtainFeeData',
                method:'post',
                where:{feeType:feeType},
                parseData: function(res){
                    return {
                        "code": res.code, //解析接口状态
                        "msg": res.codeDesc, //解析提示文本
                        "count": res.costCount, //解析数据长度
                        "data": res.data //解析数据列表
                    };
                },
                done: function(res, curr, count){
                    $("#totalCount").html(count);
                },
                page: false //开启分页
                ,
                cols: colInfo
            });
            //点击事件
            table.on('tool(test)', function(obj){
                var data = obj.data;
                if(obj.event === 'del'){
                    var costNo = data.costNo;
                    deleteCostInfo(costNo);
                } else if(obj.event === 'edit'){
                    var costNo = data.costNo;
                    updateCostInfo(costNo);
                }
            });
        });
    }
}
//费用添加页面
function addFeeInfo() {
    var feeType =$(".current").attr("id");
    var  str="";
    var nameLabel="";
    var costLabel="";
    var nameTip="";
    var costTip="";
    var nameMaxLength=0;
    if(feeType=='102'){
        nameLabel="护理等级:";
        nameTip="请输入护理等级";
        costLabel="护理费用(元/月):";
        costTip="请输入护理费用";
        nameMaxLength=16;
    }else if(feeType=='103'){
        //如果是长护险费
        nameLabel="评估等级:";
        nameTip="请输入评估等级";
        costLabel="长护险补贴费用(元/月):";
        costTip="请填写费用金额";
        nameMaxLength=16;
    }else {
        nameLabel="床位类型:";
        nameTip="请输入床位类型名称";
        costLabel="床位费(元/月):";
        costTip="请输入床位费金额";
        nameMaxLength=18;
    }
    //如果是护理费用
    str+="<div class=\"pop-up\">";
        str+="<div>";
            str+="<div class=\"pop-title relative overflow mb15px\">";
                str+="<span class=\"ml15px\">新增</span>";
                str+="<p class=\"right cursor\"><img src='"+getRootPath_web()+"static/img/close_icon.png' class=\"block pop-close\"><p>";
            str+="</div>";
            str+="<form id='costForm'>";
            str+="<div class=\"pop-form pop-form-bed mt30px mb30px\">";
                str+="<div class=\"layui-form-item\">";
                    str+="<label class=\"layui-form-label border-box\" style=\"padding-right: 10px;\"><span class=\"red\">*</span>"+nameLabel+"</label>";
                    str+="<div class=\"layui-input-block\" style=\"margin-left: 160px;\">";
                    str+="<input type=\"text\" name='costName' maxlength='"+nameMaxLength+"' placeholder='"+nameTip+"' class=\"layui-input costName-input\">";
                    str+="</div>";
                str+="</div>";
                str+="<div class=\"layui-form-item\">";
                    str+="<label class=\"layui-form-label\"><span class=\"red\">*</span>"+costLabel+"</label>";
                    str+="<div class=\"layui-input-block\">";
                        str+="<input type=\"text\" name='costPrice' onkeyup=\"value=value.replace(/^\\D*(\\d*(?:\\.\\d{0,2})?).*$/g, '$1')\" placeholder='"+costTip+"' class=\"layui-input costPrice-input\">";
                    str+="</div>";
                str+="</div>";
        if(feeType=='102'){
            str+="<div class=\"layui-form-item\">";
                str+="<label class=\"layui-form-label border-box\" style=\"padding-right: 10px;\"><span class=\"red\">*</span>护理内容上传:</label>";
                str+="<div class=\"layui-input-block\">";
                    str+="<ul>";
                    str+="<li class=\"filejs\">";
                    str+="<img src='"+getRootPath_web()+"static/img/fileTwo.png' style=\"width: 70px; max-width:617px ; height: 107px; max-height: 758px;\" class=\"border-radius4 fileImg\">";
                    str+="<input id='fileInput' name='file' accept='image/*' type=\"file\" class=\"hidden file\"/>";
                    str+="</li>";
                    str+="<li class=\"lightGray mt5px\">支持jpg/jpeg/png格式，推荐尺寸617*758px</li>";
                    str+="</ul>";
                str+="</div>";
            str+="</div>";
        }
        str+="</div>";
            str+="<div class=\"btn-con textCenter\">";
            str+="<span class=\"btn-one btn-cancel cursor\">取&nbsp;消</span>";
            str+="<span class=\"btn-two btn_submit cursor\">提&nbsp;交</span>";
            str+="</div>";
        str+="</div>";
        str+="</form>";
    str+="</div>";
    layui.use(['layer'], function() {
        var layer = layui.layer;
        //弹层
        layer.open({
            type: 1,
            title: false, //不显示标题栏
            closeBtn: false,
            area: ['500px', ''],
            shade: 0.6,
            id: 'payCons', //设定一个id，防止重复弹出
            /*,btn: ['确认支付']*/
            btnAlign: 'c',
            moveType: 1, //拖拽模式，0或者1
            shadeClose: false, //设置允许点遮罩关闭
            content:str,
            success: function() {
                //加载图片
                $(".fileImg").click(function(){
                    $(".file").click();
                })
                //图片预览
                $("#fileInput").change(function() {
                    var file = this.files[0];
                    var r = new FileReader();
                    r.readAsDataURL(file);
                    $(r).load(function() {
                        $(".fileImg").attr('src',this.result);
                    });
                });
                //关闭弹层
                $(".pop-close").click(function() {
                    layer.closeAll();
                });
                $(".btn-cancel").click(function() {
                    layer.closeAll();
                });
                $(".btn_submit").click(function () {
                    saveFeeInfo(nameLabel,costLabel,feeType);
                })
            },
        });
    });
}
//保存费用信息
function saveFeeInfo(nameLabel,costLabel,feeType){
    var costForm = new FormData(document.getElementById("costForm"));
    layui.use('layer', function(){
        var layer = layui.layer;
        //费用名称
        var costName = $(".costName-input").val();
        if(costName==''){
            layer.msg(nameLabel+'不能为空！');
            return false;
        }
        //费用金额
        var costPrice = $(".costPrice-input").val();
        if(costPrice==''){
            layer.msg(nameLabel+'不能为空！');
            return false;
        }
        if(feeType==102){
            //获取图片
            var imageFile = document.getElementById("fileInput").value;
            if (imageFile=='') {
                layer.msg('请上传护理内容！');
                return false;
            }else {
                //验证图片格式
                if (!/\.(jpg|jpeg|png|JPEG|JPG|PNG)$/.test(imageFile)) {
                    layer.msg("图片类型必须是jpeg,jpg,png中的一种");
                    return false;
                }
            }
        }
        var loading;
        costForm.append("costType",feeType);
        // layer.confirm('确定添加吗？', {
        //     btn: ['确定','取消'] //按钮
        // }, function(){
        //
        // }, function(){
        //     layer.close(layer.index);
        // });
        $.ajax({
            url:getRootPath_web()
                +'baseData/fee/saveFeeInfo',
            type:'post',
            dataType:'json',
            data:costForm,
            processData: false,
            contentType: false,
            beforeSend:function () {
                loading = layer.load(1, {
                    shade: [0.9,'#fff']
                    ,shadeClose:false
                });
            },
            success:function (result) {
                if(result.code=='0000'){
                    layer.confirm('添加成功', {
                        btn: ['确定'] //按钮
                        ,shadeClose: false
                        ,closeBtn:false
                    }, function(){
                        layer.closeAll();
                        queryFeeInfo(feeType);
                    })
                }else {
                    layer.confirm(result.codeDesc, {
                        btn: ['确定'] //按钮
                        ,shadeClose: false
                        ,closeBtn:false
                    }, function(){
                        layer.close(layer.index);
                        layer.close(loading);
                    })
                }
            }
        })
    });
}

//保存伙食费
function saveMealFeeInfo() {
    layui.use('layer', function(){
        var layer = layui.layer;
        var mealFee = $("#mealFee").val();
        if(mealFee==''){
            layer.msg('请输入伙食费！');
            return false;
        }
        var loading;
        var feeType="104";
        // layer.confirm('确定添加吗？', {
        //     btn: ['确定','取消'] //按钮
        // }, function(){
        //
        // }, function(){
        //     layer.close(layer.index);
        // });
        $.ajax({
            url:getRootPath_web()
                +'baseData/fee/saveFeeInfo',
            type:'post',
            dataType:'json',
            data:{"costType":feeType,"costPrice":mealFee},
            beforeSend:function () {
                loading = layer.load(1, {
                    shade: [0.9,'#fff']
                    ,shadeClose:false
                });
            },
            success:function (result) {
                if(result.code=='0000'){
                    layer.confirm('添加成功', {
                        btn: ['确定'] //按钮
                        ,shadeClose: false
                        ,closeBtn:false
                    }, function(){
                        layer.closeAll();
                        queryFeeInfo(feeType);
                    })
                }else {
                    layer.confirm(result.codeDesc, {
                        btn: ['确定'] //按钮
                        ,shadeClose: false
                        ,closeBtn:false
                    }, function(){
                        layer.close(layer.index);
                        layer.close(loading);
                    })
                }
            }
        })
    });
}
//修改费用信息
function updateCostInfo(costNo) {
    $.ajax({
        url:getRootPath_web()+'baseData/fee/queryFeeInfoDetail',
        type:'post',
        dataType:'json',
        data:{"costNo":costNo},
        success:function (result) {
            var feeType =result.costType;
            var  str="";
            var nameLabel="";
            var costLabel="";
            var nameTip="";
            var costTip="";
            var nameMaxLength=0;
            if(feeType=='102'){
                nameLabel="护理等级:";
                nameTip="请输入护理等级";
                costLabel="护理费用(元/月):";
                costTip="请输入护理费用";
                nameMaxLength=16;
            }else if(feeType=='103'){
                //如果是长护险费
                nameLabel="评估等级:";
                nameTip="请输入评估等级";
                costLabel="长护险补贴费用(元/月):";
                costTip="请填写费用金额";
                nameMaxLength=16;
            }else {
                nameLabel="床位类型:";
                nameTip="请输入床位类型名称";
                costLabel="床位费(元/月):";
                costTip="请输入床位费金额";
                nameMaxLength=18;
            }
            //如果是护理费用
            str+="<div class=\"pop-up\">";
            str+="<div>";
            str+="<div class=\"pop-title relative overflow mb15px\">";
            str+="<span class=\"ml15px\">编辑</span>";
            str+="<p class=\"right cursor\"><img src='"+getRootPath_web()+"static/img/close_icon.png' class=\"block pop-close\"><p>";
            str+="</div>";
            str+="<form id='costForm'>";
            str+="<input name='costNo' type='hidden' value='"+result.costNo+"' />";
            str+="<div class=\"pop-form pop-form-bed mt30px mb30px\">";
            str+="<div class=\"layui-form-item\">";
            str+="<label class=\"layui-form-label border-box\" style=\"padding-right: 10px;\"><span class=\"red\">*</span>"+nameLabel+"</label>";
            str+="<div class=\"layui-input-block\" style=\"margin-left: 160px;\">";
            str+="<input type=\"text\" value='"+result.costName+"' name='costName' maxlength='"+nameMaxLength+"' placeholder='"+nameTip+"' class=\"layui-input costName-input\">";
            str+="</div>";
            str+="</div>";
            str+="<div class=\"layui-form-item\">";
            str+="<label class=\"layui-form-label\"><span class=\"red\">*</span>"+costLabel+"</label>";
            str+="<div class=\"layui-input-block\">";
            str+="<input type=\"text\" value='"+result.costPrice+"' name='costPrice' onkeyup=\"value=value.replace(/^\\D*(\\d*(?:\\.\\d{0,2})?).*$/g, '$1')\" placeholder='"+costTip+"' class=\"layui-input costPrice-input\">";
            str+="</div>";
            str+="</div>";
            if(feeType=='102'){
                str+="<div class=\"layui-form-item\">";
                str+="<label class=\"layui-form-label border-box\" style=\"padding-right: 10px;\"><span class=\"red\">*</span>护理内容上传:</label>";
                str+="<div class=\"layui-input-block\">";
                str+="<ul>";
                str+="<li class=\"filejs\">";
                if(result.costImg==''){
                    str+="<img src='"+getRootPath_web()+"static/img/fileTwo.png' style=\"width: 70px; max-width:617px ; height: 107px; max-height: 758px;\" class=\"border-radius4 fileImg\">";
                }else {
                    str+="<img src='"+result.costImg+"' style=\"width: 70px; max-width:617px ; height: 107px; max-height: 758px;\" class=\"border-radius4 fileImg\">";
                }
                str+="<input id='fileInput' name='file' accept='image/*' type=\"file\" class=\"hidden file\"/>";
                str+="</li>";
                str+="<li class=\"lightGray mt5px\">支持jpg/jpeg/png格式，推荐尺寸617*758px</li>";
                str+="</ul>";
                str+="</div>";
                str+="</div>";
            }
            str+="</div>";
            str+="<div class=\"btn-con textCenter\">";
            str+="<span class=\"btn-one btn-cancel cursor\">取&nbsp;消</span>";
            str+="<span class=\"btn-two btn_submit cursor\">修&nbsp;改</span>";
            str+="</div>";
            str+="</div>";
            str+="</form>";
            str+="</div>";
            layui.use(['layer'], function() {
                var layer = layui.layer;
                //弹层
                layer.open({
                    type: 1,
                    title: false, //不显示标题栏
                    closeBtn: false,
                    area: ['500px', ''],
                    shade: 0.6,
                    id: 'payCons', //设定一个id，防止重复弹出
                    /*,btn: ['确认支付']*/
                    btnAlign: 'c',
                    moveType: 1, //拖拽模式，0或者1
                    shadeClose: false, //设置允许点遮罩关闭
                    content:str,
                    success: function() {
                        //加载图片
                        $(".fileImg").click(function(){
                            $(".file").click();
                        })
                        //图片预览
                        $("#fileInput").change(function() {
                            var file = this.files[0];
                            var r = new FileReader();
                            r.readAsDataURL(file);
                            $(r).load(function() {
                                $(".fileImg").attr('src',this.result);
                            });
                        });
                        //关闭弹层
                        $(".pop-close").click(function() {
                            layer.closeAll();
                        });
                        $(".btn-cancel").click(function() {
                            layer.closeAll();
                        });
                        $(".btn_submit").click(function () {
                            saveUpdateFeeInfo(nameLabel,costLabel,feeType);
                        })
                    },
                });
            });
        }
    })
}
//保存修改后的费用信息
function saveUpdateFeeInfo(nameLabel,costLabel,feeType) {
    var costForm = new FormData(document.getElementById("costForm"));
    layui.use('layer', function(){
        var layer = layui.layer;
        //费用名称
        var costName = $(".costName-input").val();
        if(costName==''){
            layer.msg(nameLabel+'不能为空！');
            return false;
        }
        //费用金额
        var costPrice = $(".costPrice-input").val();
        if(costPrice==''){
            layer.msg(nameLabel+'不能为空！');
            return false;
        }
        if(feeType==102){
            //获取图片
            var imageFile = document.getElementById("fileInput").value;
            if (imageFile!='') {
                //验证图片格式
                if (!/\.(jpg|jpeg|png|JPEG|JPG|PNG)$/.test(imageFile)) {
                    layer.msg("图片类型必须是jpeg,jpg,png中的一种");
                    return false;
                }
            }
        }
        var loading;
        costForm.append("costType",feeType);
        // layer.confirm('确定修改吗？', {
        //     btn: ['确定','取消'] //按钮
        // }, function(){
        //
        // }, function(){
        //     layer.close(layer.index);
        // });
        $.ajax({
            url:getRootPath_web()
                +'baseData/fee/updateFeeInfo',
            type:'post',
            dataType:'json',
            data:costForm,
            processData: false,
            contentType: false,
            beforeSend:function () {
                loading = layer.load(1, {
                    shade: [0.9,'#fff']
                    ,shadeClose:false
                });
            },
            success:function (result) {
                if(result.code=='0000'){
                    layer.confirm('修改成功', {
                        btn: ['确定'] //按钮
                        ,shadeClose: false
                        ,closeBtn:false
                    }, function(){
                        layer.closeAll();
                        queryFeeInfo(feeType);
                    })
                }else {
                    layer.confirm(result.codeDesc, {
                        btn: ['确定'] //按钮
                        ,shadeClose: false
                        ,closeBtn:false
                    }, function(){
                        layer.close(layer.index);
                        layer.close(loading);
                    })
                }
            }
        })
    });
}
//删除费用信息
function deleteCostInfo(costNo) {
    layui.use('layer', function(){
        var layer = layui.layer;
        layer.confirm('确定删除吗？', {
            btn: ['确定','取消'] //按钮
        }, function(){
            $.ajax({
                url:getRootPath_web()
                    +'baseData/fee/deleteFeeInfo',
                type:'post',
                dataType:'json',
                data:{"costNo":costNo},
                success:function (result) {
                    if(result.code=='0000'){
                        layer.confirm('删除成功', {
                            btn: ['确定'] //按钮
                            ,shadeClose: false
                        }, function(){
                            layer.closeAll();
                            queryFeeInfo(result.costType);
                        })
                    }else {
                        layer.confirm(result.codeDesc, {
                            btn: ['确定'] //按钮
                            ,shadeClose: false
                        }, function(){
                            layer.close(layer.index);
                        })
                    }
                }
            })
        }, function(){
            layer.close(layer.index);
        });
    });
}